package ik.xcj.hostpital.manage.controller;


import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import ik.xcj.hostpital.manage.pojo.dto.*;
import ik.xcj.hostpital.manage.pojo.vo.UserListItemVO;
import ik.xcj.hostpital.manage.pojo.vo.UserStandardVO;
import ik.xcj.hostpital.manage.service.UserService;
import ik.xcj.hostpital.manage.web.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Slf4j
@RestController
@Api(tags = "用户管理模块")
@RequestMapping("/users")
public class UserController {
    @Autowired
    private UserService iUserService;

    public UserController(){
        log.debug("开始创建控制器类对象");
    }



    @PostMapping("/login")
    @ApiOperation("用户登录")
    public JsonResult<String> login(UserLoginDTO userLoginDTO){
        log.debug("开始处理【管理员登录】的请求，参数：{}", userLoginDTO);
        String jwt = iUserService.login(userLoginDTO);
        return JsonResult.ok(jwt);
    }

    @PostMapping("/register")
    @ApiOperation("注册用户")
    @ApiOperationSupport(order = 100)
    public JsonResult<Void> register(@Valid UserRegDTO userRegDTO){
        log.debug("开始处理【添加用户】的请求，参数：{}",userRegDTO);
        iUserService.insert(userRegDTO);
        return JsonResult.ok();
    }


    @PostMapping("/login/phone")
    @ApiOperation("手机号码登录")
    @ApiOperationSupport(order = 51)
    public JsonResult loginByPhone(@Valid UserLoginByPhoneDTO userLoginByPhoneDTO){
        log.debug("开始处理【用户登录】的请求，参数：{}",userLoginByPhoneDTO);
        iUserService.loginByPhone(userLoginByPhoneDTO);
        return JsonResult.ok();
    }

    @ApiOperation("发送手机号验证码")
    @ApiOperationSupport(order = 105)
    @GetMapping("/login/phone/send/{phone}")
    public JsonResult<Void> sendByPhone(@PathVariable String phone){
        System.out.println("phone = " + phone);
        iUserService.senPhone(phone);
        return JsonResult.ok();
    }
    @PostMapping("/{id:[0-9]+}/delete")
    @ApiOperation("根据id删除用户")
    @ApiOperationSupport(order = 200)
    public JsonResult delete(@Range(min = 1, max = 1000000, message = "删除用户失败，用户ID非法！")
                             @PathVariable Long id){
        log.debug("开始处理【根据id删除用户】的请求，参数：{}",id);
        iUserService.deleteById(id);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/update")
    @ApiOperation("修改用户数据")
    @ApiOperationSupport(order = 300)
    public JsonResult updateInfoById(@PathVariable Long id, UserUpdateDTO userUpdateDTO) {
        log.debug("开始处理【修改用户详情】的请求，参数：{}", userUpdateDTO);
        iUserService.updateInfoById(id, userUpdateDTO);
        return JsonResult.ok();
    }
    @GetMapping("/{id:[0-9]+}")
    @ApiOperation("根据id查询用户详情")
    @ApiOperationSupport(order = 410)
    public JsonResult<UserStandardVO> getStandardById(@PathVariable Long id) {
        log.debug("开始处理【根据id查询用户详情】的请求，参数：{}", id);
        UserStandardVO userStandardById = iUserService.getStandardById(id);
        return JsonResult.ok(userStandardById);
    }

    @GetMapping("/getStandardByUsername/{username}")
    @ApiOperation("根据用户名查询用户详情")
    @ApiOperationSupport(order = 411)
    public JsonResult<UserStandardVO> getStandardByUsername(@PathVariable String username) {
        log.debug("开始处理【根据用户名查询用户详情】的请求，参数：{}", username);
        UserStandardVO userStandardByUsername = iUserService.getStandardByUsername(username);
        return JsonResult.ok(userStandardByUsername);
    }
    @GetMapping("/getStandardByPhone/{phone}")
    @ApiOperation("根据手机号查询用户详情")
    @ApiOperationSupport(order = 412)
    public JsonResult<UserStandardVO> getStandardByPhone(@PathVariable String phone) {
        log.debug("开始处理【根据手机号查询用户详情】的请求，参数：{}", phone);
        UserStandardVO userStandardByPhone = iUserService.getStandardByPhone(phone);
        return JsonResult.ok(userStandardByPhone);
    }
    @GetMapping("")
    @ApiOperation("查询用户列表")
    @ApiOperationSupport(order = 420)
    public JsonResult<List<UserListItemVO>> list() {
        log.debug("开始处理【查询用户列表】的业务，无参数");
        List<UserListItemVO> list = iUserService.list();
        return JsonResult.ok(list);
    }


}
